<?php
namespace App\Admin\Controller;

use Foxforms\Widget\Field\Radio;
use Foxforms\Widget\Field\Select;
use Foxforms\Widget\Field\Input;
use Foxforms\Widget\Form;

class centrocustoController extends \Foxframe\Controller
{

	public function viewListar($params = array())
	{
		$this->view->assign('lista', $this->em->getRepository('\Entities\Corp\CentroCusto')->findAll());
	}

	public function viewSalvar($params = array())
	{
		if (isset($params['id']) && !empty($params['id']))
			$centroCusto	= $this->em->find('\Entities\Corp\CentroCusto', $params['id']);
		else
			$centroCusto	= new \Entities\Corp\CentroCusto();

		$form	= new Form();
		$form->addValues($centroCusto, 'centrocusto');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('admin'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('centrocusto'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveSalvar'));
		$form->addElement(Input::create('centrocusto[version]')->setType('hidden'));
		$form->addElement(Input::create('centrocusto[id_centro_custo]')->setType('hidden'));
		$form->addElement(Input::create('centrocusto[ds_nome]')->validate()->setLabel('Nome'));
		$form->addElement(Input::create('centrocusto[nr_codigo]')->validate()->setLabel('Codigo'));
		$form->addElement(
				Radio::create('centrocusto[fl_status]')
				->setLabel('Status')
				->setPlaceHolder('Selecione')
				->setOption(array(0 => 'Inativo', 1 => 'Ativo'))
				->validate()
		);
		$form->addElement(
				Select::create('centrocusto[subordinador]')
				->setLabel('Subordinador')
				->setPlaceHolder('--Selecione--')
				->setOption($this->em->getRepository('\Entities\Corp\CentroCusto')->listCombo())
		);
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);
		return $this->returnAjax();
	}

	public function saveSalvar($params = array())
	{
		try
		{
			$centroCusto	= new \Foxframe\Service();
			$centroCusto->save($params['centrocusto'], '\Entities\Corp\CentroCusto');
			$this->em->flush();
			$this->setFlash('Success', 'Sucesso', 'Centro de custos salvo com sucesso!');
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro', $e->getMessage());
		}
		$this->redirect($this->uri->getOrigem());
	}
}